#class MmpiLoader(object):
#    def __init__ (self):
#        self.lines = []
#
#    def LoadfromFile(self, filepath):
#    # allows for loading a specific file
#        self.lines = []
#        for line in open(filepath):
#            self.lines.append(line)
#        self.lines = LinesToFix

class MmpiExtractor(object):
# contains function that loads the lines and also contains the extractor functions
    def __init__ (self):
        self.lines = []
        self.d = {}
        self.ProRS = {}
        self.ProTS = {}
        self.SupRS = {}
        self.SupTS = {}
        self.ConRS = {}
        self.ConTS = {}
        self.NonK = {}
        self.NonKscale = ['HS', 'PD', 'PT', 'SC', 'MA']
        self.DpR = {}
        self.DpT = {}
        self.HyR = {}
        self.HyT = {}
        self.PdR = {}
        self.PdT = {}
        self.PaR = {}
        self.PaT = {}
        self.ScR = {}
        self.ScT = {}
        self.MaR = {}
        self.MaT = {}
        self.SiR = {}
        self.SiT = {}

    def LoadfromFile(self, filepath):
    # allows for loading a specific file
        self.lines = []
        for line in open(filepath):
            self.lines.append(line)

    def loadLines(self, linesToLoad):
        # load a file into a list
        self.lines = linesToLoad

    def extractRaw(self, lines):
        # extract raw scores from lines
        inIR = False
        for line in self.lines:
            if line.rfind('--- ITEM RESPONSES ---') >= 0:
                inIR = True
                continue
            if inIR == True:
                scale = line.split()
                if scale == None or len(scale) <= 1:
                    continue
                for p in range(0, len(scale), 2):
                    self.d["ir%03d"%(int(scale[p]))] = scale[p+1]

    def extractScales(self, lines):
        NonKscale = ['HS', 'PD', 'PT', 'SC', 'MA']
        # extracts scores from MMPI scales
        for i in range(0, len(self.lines)):
            if lines[i].rfind('L   F   K   HS  D   HY  PD  MF  PA  PT  SC  MA  SI') >= 0:
                scale = lines[i].split()
                rscore = lines[i+1].split()
                tscore = lines[i+5].split()
                rfinal = rscore[2:]
                tfinal = tscore[2:]
                for q in range (0, 12):
                    self.ProRS["proraw%s"%(str(scale[q]))] = rfinal[q]
                    self.ProTS["prot%s"%(str(scale[q]))] = tfinal[q]

                continue

            if lines[i].rfind('A   R   Es  FB  TR  VR  OH  Do  RE  Mt  GM  GF  PK  PS') >= 0:
                scale = lines[i].split()
                scale[-1] = 'MACR'
                rscore = lines[i+2].split()
                tscore = lines[i+4].split()
                rfinal = rscore[1:]
                tfinal = tscore[2:]
                for q in range (0, 15):
                    self.SupRS["supraw%s"%(str(scale[q]))] = rfinal[q]
                    self.SupTS["supt%s"%(str(scale[q]))] = tfinal[q]

                continue
            if lines[i].rfind('ANX FRS OBS DEP HEA BIZ ANG CYN ASP TPA') >= 0:
                scale =  lines[i].split()
                rscore = lines[i+2].split()
                tscore = lines[i+4].split()
                rfinal = rscore[1:]
                tfinal = tscore[2:]
                for q in range (0, 15):
                    self.ConRS["conraw%s"%(str(scale[q]))] = rfinal[q]
                    self.ConTS["cont%s"%(str(scale[q]))] = tfinal[q]

                continue
            if lines[i].rfind('K and Non-K Corrected Profile') >= 0:
                scale = lines[i+55].split()
                NonKcorr = scale[2:]
                for q in range(0, 5):
                    self.NonK["NonKScore%s"%(str(NonKscale[q]))] = NonKcorr[q]

                break

    def extractSubscales(self, lines):
    #insert the code for extracting the subscales
        for i in range(0, len(lines)):
            if lines[i].rfind('Depression Subscales') >= 0:
                for q in range(0, 5):
                    Dpscale = lines[i+q+2].split()
                    DpsubR = Dpscale[-2]
                    DpsubT = Dpscale[-1]
                    self.DpR["DR%s"%(str([q+1]))] = DpsubR
                    self.DpT["DT%s"%(str([q+1]))] = DpsubT

            if lines[i].rfind('Hysteria Subscales') >= 0:
                for q in range(0, 5):
                    Hyscale = lines[i+q+2].split()
                    HysubR = Hyscale[-2]
                    HysubT = Hyscale[-1]
                    self.HyR["HyR%s"%(str([q+1]))] = HysubR
                    self.HyT["HyT%s"%(str([q+1]))] = HysubT


            if lines[i].rfind('Psychopathic Deviate Subscales') >= 0:
                for q in range(0, 5):
                    Pdscale = lines[i+q+4].split()
                    PdsubR = Pdscale[-2]
                    PdsubT = Pdscale[-1]
                    self.PdR["PdR%s"%(str([q+1]))] = PdsubR
                    self.PdT["PdT%s"%(str([q+1]))] = PdsubT

            if lines[i].rfind('Paranoia Subscales') >= 0:
                for q in range(0, 3):
                    Pascale = lines[i+q+2].split()
                    PasubR = Pascale[-2]
                    PasubT = Pascale[-1]
                    self.PaR["PaR%s"%(str([q+1]))] = PasubR
                    self.PaT["PaT%s"%(str([q+1]))] = PasubT

            if lines[i].rfind('Schizophrenia Subscales') >= 0:
                for q in range(0, 6):
                    Scscale = lines[i+q+2].split()
                    ScsubR = Scscale[-2]
                    ScsubT = Scscale[-1]
                    self.ScR["ScR%s"%(str([q+1]))] = ScsubR
                    self.ScT["ScT%s"%(str([q+1]))] = ScsubT

            if lines[i].rfind('Hypomania Subscales') >= 0:
                for q in range(0, 4):
                    Mascale = lines[i+q+2].split()
                    MasubR = Mascale[-2]
                    MasubT = Mascale[-1]
                    self.MaR["MaR%s"%(str([q+1]))] = MasubR
                    self.MaT["MaT%s"%(str([q+1]))] = MasubT

            if lines[i].rfind('Social Introversion Subscales') >= 0:
                for q in range(0, 3):
                    Siscale = lines[i+q+4].split()
                    SisubR = Siscale[-2]
                    SisubT = Siscale[-1]
                    self.SiR["SiR%s"%(str([q+1]))] = SisubR
                    self.SiT["SiT%s"%(str([q+1]))] = SisubT









